(function($){
	$.fn.galleryView = function(options) {
		var opts = $.extend($.fn.galleryView.defaults,options);
		var id;
		var iterator = 0;
		var item_count = 0;
		var slide_method;
		var j_gallery;
		var j_filmstrip;
		var j_frames;
		var j_panels;
		var j_pointer;
		var j_url;
		function showItem(i) {
			if(has_panels) {
				j_panels.fadeOut(0).eq(i%item_count).fadeIn(0);
			}
			if(has_filmstrip) {
				if(slide_method=='pointer') {
					j_pointer.stop();
					var pos = getPos(j_frames[i]);
					j_pointer.css({
						'left':(pos.left+1+'px')
					},0);
				}
			}
		};
		function showNextItem() {
			$(document).stopTime("transition");
			if(++iterator==j_frames.length) {iterator=0;}
			showItem(iterator);
			$(document).everyTime(opts.transition_interval,"transition",function(){
				showNextItem();
			});
		};
		function getPos(el) {
			var left = 0, top = 0;
			var el_id = el.id;
			if(el.offsetParent) {
				do {
					left += el.offsetLeft;
					top += el.offsetTop;
				} while(el = el.offsetParent);
			}
			if(el_id == id) {return {'left':left,'top':top};}
			else {
				var gPos = getPos(j_gallery[0]);
				var gLeft = gPos.left;
				var gTop = gPos.top;

				return {'left':left-gLeft,'top':top-gTop};
			}
		};
		function enableFrameClicking() {
			j_frames.each(function(i){
					$(this).mouseover(function(){
						$(document).stopTime("transition");
						showItem(i);
						iterator = i;
						j_url = $('a',$(this)).attr("href");
						$('.pointa').attr('href',j_url);
						$('.pointa').attr('target','_blank');
						$(document).everyTime(opts.transition_interval,"transition",function(){
							showNextItem();
						});
					});
			});
		};
		function buildFilmstrip() {
			var pointer = $('<div></div>');
			pointer.attr('id','pointer').appendTo(j_gallery).css({
				 'left':getPos(j_frames[0]).left+1+'px'
			});
			j_pointer = $('#pointer',j_gallery);
			var pointerArrow = $('<div class="pointer"></div>');
			pointerArrow.appendTo($('#pointer'));
			f_url = $('.filmstrip li:first a').attr("href");
			var pointer_a = $('<a href=\"'+f_url+'\" class="pointa"></a>');
			pointer_a.appendTo($('#pointer'));
			pointer.mouseover(function(){
				$(document).stopTime("transition");
			});
			pointer.mouseout(function(){
				$(document).everyTime(opts.transition_interval,"transition",function(){
					showNextItem();
				});
			});
		};


	return this.each(function() {
		j_gallery = $(this);
		j_gallery.css('visibility','hidden');
		j_filmstrip = $('.filmstrip',j_gallery);
		j_frames = $('li',j_filmstrip);
		j_panels = $('.panel',j_gallery);

		j_panels.mouseover(function(){
			$(document).stopTime("transition");
		});
		j_panels.mouseout(function(){
			$(document).everyTime(opts.transition_interval,"transition",function(){
						showNextItem();
					});
		});

		id = j_gallery.attr('id');
		has_panels = j_panels.length > 0;
		has_filmstrip = j_frames.length > 0;
		item_count = has_panels?j_panels.length:j_frames.length;
		strip_size = 6;
		if(strip_size >= item_count) {
			slide_method = 'pointer';
			strip_size = item_count;
		}
		if(has_filmstrip) {
			buildFilmstrip();
		}
		if(has_filmstrip) enableFrameClicking();

		j_panels.eq(0).show();
		if(item_count > 1) {
			$(document).everyTime(opts.transition_interval,"transition",function(){
				showNextItem();
			});
		}
		j_gallery.css('visibility','visible');
		});
	};
	$.fn.galleryView.defaults = {
		transition_interval: 3000
	};
})(jQuery)

function widget(sAct, oData){
	/*公用部件--video/img/blog频道页轮播*/
        var i = 0, f = $('.picshow'), l = f.find('.current_pic .focus').length, s = '';
        function num() {
            for (var j = 0; j < l; j++) {
                s += '<a href="" title="第' + (j + 1) + '张">' + (j + 1) + '</a>';
            }
            num_w = 28 * l;
            f.find('.num').append(s).width(num_w).find('a').removeClass('ac').hover(function () {
                clearTimeout(t);
                var _self = $(this);
                _self.addClass('ac');
                i = f.find('.num a').index(_self[0]);
                g(sAct, oData);
				clearTimeout(t);
                return false;
            }, function () { t=setTimeout(function () { g(sAct, oData) }, oData.timer); }).click(function () { return false; });
			$('.current_pic').hover(function(){clearTimeout(t);},function (){t=setTimeout(function () { g(sAct, oData) }, oData.timer);});
        }
        function g(sAct, oData) {
            f.find('.num a').removeClass('ac');
            if (i < l) {
                switch (sAct) {
                    case 'slide':
                        f.find('.thumb_bg').animate({ left: -367 * i }, 300);
                        f.find('.current_pic .row').animate({ left: oData.distance * i }, oData.speed);
                }
                f.find('.num a:eq(' + i + ')').addClass('ac');
                i++;
                t = setTimeout(function () { g(sAct, oData) }, oData.timer);
            }
            else { i = 0; g(sAct, oData); }
        }
        if (l) { f.find('.current_pic .row').width(oData.width * (l + 1)); num(); g(sAct, oData); }
}
function rutunFocus(){
	widget('slide',{'mode':'simple','distance':-667,'width':662,'height':330,'speed':300,'timer':3000});
}